var httpPort = process.env['app_port'] || 8080;
var isWindows = httpPort == 8080;
var dirSep = isWindows ? '\\' : '/';
var devEnv = process.env['NODE_ENV'] !== 'production';

var sys = require('sys');
var fs = require('fs');

var connect = require('connect');
var assetManager = require('connect-assetmanager');
var assetHandler = require('connect-assetmanager-handlers');

var static = __dirname + dirSep + 'static';

var logFile = function(filename) {
    console.log('reading file:' +filename);
    var content = fs.readFileSync(filename, 'utf8');
    console.log(content);
}

var assetManagerGroups = {
    'helper':{
        'route':/\/js\/[0-9]+\/utils\.js/,
        'path':static + '/js/',
        'dataType':'javascript',
        'files':['Helper.js','math.js'],
        debug: devEnv,
        stale: !devEnv
    },
    'figures' : {
        'route':/\/js\/[0-9]+\/figures\.js/,
        'path':static + '/js/figures/',
        'dataType':'javascript',
        'files':[
            '*'
        ],
        debug: devEnv,
        stale: !devEnv
    },
    'actions' : {
        'route':/\/js\/[0-9]+\/actions\.js/,
        'path':static + '/js/behaviours/',
        'dataType':'javascript',
        'files':[
            '*'
        ],
        debug: devEnv,
        stale: !devEnv
    }
};



var assetsManagerMiddleware = assetManager(assetManagerGroups);
var app = connect()

    .use(assetsManagerMiddleware)
    .use(connect.static(static))
    .listen(httpPort);

console.log('started on port:' + httpPort);
console.log('static dir: ' + static);
console.log('is debugging: ' + devEnv);